package thread;

/*
 * @author zzr
 * @date: 2025/03/30  20:52
 * @description:
 */
public class ThreadDemo19 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (A) {
                // sleep 一下, 是给 t2 时间, 让 t2 也能拿到 B
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 尝试获取 B, 并没有释放 A
                synchronized (B) {
                    System.out.println("t1 拿到了两把锁!");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (A) {
                // sleep 一下, 是给 t1 时间, 让 t1 能拿到 A
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 尝试获取 A, 并没有释放 B
                synchronized (B) {
                    System.out.println("t2 拿到了两把锁!");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
